<?php
	/**
	* Ads 
	* 
	* Ads is the controller, it executes the route methods, loads the models and executes the methods in model. 
	* Ads Controller executes the tasks for ads, ads assign actions include
	* list, create, update, delete the ads
	* list, assign the ads text to the kiosk
	* 
	* @author Thang
	* @version $Id:  ads.php 2012-04-13
	* @access public 
	*
	*/
	class Ads extends MY_Controller{
		
		public function __construct(){
			parent::__construct();
			$this->load->model('ads_model');
			$this->load->model('kiosk_model');
			$this->load->model('ads_assign_model');
			$this->load->model('ads_broadcast_model');
		}
		
		/**
		* index(): 	this method excutes for listing data. It is as default method
		*			data loads to the $data array with define index
					load the view to display data
		* 
		* @param    
		* @return    
		* @access public
		*/
		public function index(){					
			$data['ads'] = $this->ads_model->getAdsList();
			$data['title'] = "Ad list";
			$this->load->view('templates/header1', $data);
			$this->load->view('ads/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');			
		}
		
		
		/**
		* displayForm(): 	display the form for creating new ads and update existing ads					
		* 
		* @param    int $id is the id of ads, using for updating record
		* @return    
		* @access public
		*/
		public function displayForm($id=FALSE){
				// load the form library validation and form helper
				$this->load->library('form_validation');
				$this->load->helper("form");
				
				$data = array();
				// has the id, get the ads data with the id
				if($id!=FALSE){
					$data['ads'] = $this->ads_model->getAdsList($id);				
				}
				
				// display view
				$data['title']="Ads";
				$this->load->view('templates/header1', $data);
				$this->load->view('ads/form', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			
		}
		
	
		/**
		* create(): 	this method excutes the route for creating new ads
		*				call the displayForm to display the blank form
		* 
		* @param    
		* @return    
		* @access public
		*/
		public function create(){
			$this->displayForm();
		}
		
		
		/**
		* create(): 	this method excutes the route for editing existing item
		*				call the displayForm to display the form with related data
		* 
		* @param    	int $id is the id of ads
		* @return    
		* @access public
		*/
		public function edit($id){
			$this->displayForm($id);
		}
		
		
		/**
		* delete(): 	this method excutes the route for deleting existing item
		*				call the delete methods from ads_model and passed the $id for deleting
		*				After deleting, redirect to index() method
		* 
		* @param    	int $id is the id of ads
		* @return    
		* @access public
		*/
		public function delete($id){
			$this->ads_model->delete($id);
			redirect("ads", "refresh");
		}
		
		
		/**
		* save(): 		this method excutes the route for saving data
		*				call the save method from ads_model to store data
		*				After saving, redirect to index() method
		* 
		* @param    	
		* @return    
		* @access public
		*/
		public function save(){		
				// load the form library validation and form helper
				$this->load->helper("form");
				$this->load->library('form_validation');
				
				// validate the ad_text with required rule
				$this->form_validation->set_rules('ad_text', 'Ad Text', 'required');
				
				// run the validation
				if ($this->form_validation->run() === FALSE){	// validate return false, display the form again
					$data['title']="Create Ads";
					$this->load->view('templates/header1', $data);
					$this->load->view('ads/form', $data);
					$this->load->view('templates/right', $data);
					$this->load->view('templates/footer');
				}
				else{	// save the data by model's method
					$this->ads_model->save();
					redirect("ads", "refresh");
				}			
		}
		
		
		/**
		* assign(): 	this method excutes the route for assigning ads to the kiosk
		*				Actions:
		*					+ Load the ads data from getAdsList() methods of Ads_model
		*					+ Load the kiosk data from get_kiosk_list() methods of Kiosk_model
		*					+ Load the time, minute, second data from getHours(), getMins() and getSecs() methods
		* 
		* @param    	int $id is ads_id of ads_assign
		* @return    
		* @access public
		*/		
		public function assign($id=FALSE){
			
			$data = $this->prepare_assign_data($id);
			// load the form library validation and form helper
			$this->load->helper("form");
			$this->load->library('form_validation');
			
			$data['title']="Ads Assign";
			$this->load->view('templates/header1', $data);
			$this->load->view('ads/ads_assign', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}
		
		
		/**
		* save_assign(): 	this method excutes the route for saving ads_assign
		*					by save() methods of Ads_assign_model model
		*						
		* 
		* @param    	
		* @return    
		* @access public
		*/
		public function save_assign(){
				// load the form library validation and form helper
				$this->load->helper("form");
				$this->load->library('form_validation');
				
				// validate the kiosk_id with required rule
				$this->form_validation->set_rules('kiosk_id', 'Kiosk', 'required');
				
				// validate the start_date with date_check() defined rule
				$this->form_validation->set_rules('start_date', 'Start date', 'callback_date_check');				
				
				// the validation return FALSE: get all related data again and display the input form again
				if ($this->form_validation->run() === FALSE){
					$id = $this->input->post('id');
					$data = $this->prepare_assign_data($id);					
					$data['title']="Ads Assign";
					$this->load->view('templates/header1', $data);
					$this->load->view('ads/ads_assign', $data);
					$this->load->view('templates/right', $data);
					$this->load->view('templates/footer');
				}
				else{
					$this->ads_assign_model->save();
					redirect("ads", "refresh");
				}			
		}
		
		
		/**
		* prepare_assign_data():  load data to assign ads form
		*					
		*						
		* 
		* @param    	int $id ads id
		* @return    	array - collect data
		* @access public
		*/
		public function prepare_assign_data($id){
			$data = array();

			$data['ads_id']	= $id;
			
			// load ads data to display ads text, ads id
			$data['ads'] 	= $this->ads_model->getAdsList($id);
			
			// load hour, minute, second
			$data['h'] 		= $this->getHours();
			$data['i']			= $this->getMins();
			$data['s']		= $this->getSecs();		
			$data['kiosk']  	= array();		
			
			// load ads assigned data such as start date, end date, status
			if($id!=FALSE){
				$data['ads_assign'] 	= $this->ads_assign_model->getAdsAssignList($id);
				
			}
						
			// preapared kiosk that assigned by ads _id
			if(count($data['ads_assign'])){
				$kiosk_assign = array();
				$ids = "";
				foreach($data['ads_assign'] as $v){
						$kiosk_assign[] = array('id' => $v['kiosk_id'], 'mac_address' => $v['mac_address']);
						$ids.= $v['kiosk_id'].",";
				}
				// load kiosk list and not get the item in the assigned "ids" list ex:1,2,3
				$ids = substr($ids, 0, strlen($ids)-1);
				$data['kiosk']  = $this->kiosk_model->get_kiosk_list_assign($ids);
				
				// store the "ids" list with mac_addess to array
				$data['kiosk_assign'] = $kiosk_assign;	
				
				// store the "ids" list as json
				$data['kiosk_assign_json'] =  json_encode($kiosk_assign);			
			}
			else $data['kiosk'] = $this->kiosk_model->get_kiosk_list(); 
			
			$data['kiosk_json'] = json_encode($data['kiosk']);
			
			// check if it has data from submit form
			$data['ads_assign']['start_date'] 	= $this->input->post('start_date');
			$data['ads_assign']['end_date']	= $this->input->post('end_date');
			$data['ads_assign']['start_h']		= $this->input->post('start_h');
			$data['ads_assign']['start_i']		= $this->input->post('start_i');
			$data['ads_assign']['start_s']		= $this->input->post('start_s');			
			return $data;
		}
		
		
		/**
		* getHours(): 		This method load the hour to array
		*						
		* 
		* @param    	
		* @return    		array
		* @access public
		*/
		public function getHours(){
			$arr = array();
			for($i=1; $i<=24; $i++){
				$arr[] = $i;
			}
			return $arr;
		}
		
		
		/**
		* getMins(): 		This method load the minutes to array
		*						
		* 
		* @param    	
		* @return    		array
		* @access public
		*/
		public function getMins(){
			$arr = array();
			for($i=0; $i<=59; $i++){
				$arr[] = $i;
			}
			return $arr;
		}
		
		
		/**
		* getSecs(): 		This method load the second to array
		*						
		* 
		* @param    	
		* @return    		array
		* @access public
		*/
		public function getSecs(){
			$arr = array();
			for($i=0; $i<=59; $i++){
				$arr[] = $i;
			}
			return $arr;
		}
		
		
		/**
		* date_check(): 	This method is defined rule for checking start date and end date
		*					Action:
		*						+ Check the start date and end date is required
		*						+ Check the start date should have to be lower than end date
		* 
		* @param    	
		* @return    		boolean
		* @access public
		*/
		public function date_check($start_date){
			// Get data form input form field
			$chk1 = trim($this->input->post('start_date'));
			$chk2 = trim($this->input->post('end_date'));
			
			// Required validation
			if(empty($chk1) || empty($chk2)){
				$this->form_validation->set_message('date_check', 'The start date and end date is required');
				return FALSE;
			}
			
			// Append date, hour, minute and second as date-time format
			$start_date	= $this->input->post('start_date')." ".$this->input->post('start_h').":".$this->input->post('start_i').":".$this->input->post('start_s');
			$end_date 	= $this->input->post('end_date')." ".$this->input->post('end_h').":".$this->input->post('end_i').":".$this->input->post('end_s');
			
			// Compare date validation
			if(strtotime($start_date) >= strtotime($end_date)){
				$this->form_validation->set_message('date_check', 'End date should be greater start date');
				return FALSE;
			}
			else return TRUE;
		}
		
		
		/**
		* assign_list(): 	This method displays the ads_assigned list of the ads
		*					
		* 
		* @param    	
		* @return    		
		* @access public
		*/
		public function assign_list($id){
			$data['ads_assign'] = $this->ads_assign_model->getAdsAssignListGroup($id);
			$data['title'] = "Assign list";
			$this->load->view('templates/header1', $data);
			$this->load->view('ads/assign_list', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}
		
		public function assign_edit($ads_id, $id){
			// load the form library validation and form helper
			$this->load->helper("form");
			$this->load->library('form_validation');
			
			// load the related data to form
			$data = $this->assign_edit_prepare($ads_id, $id);
			
			$this->load->view('templates/header1', $data);
			$this->load->view('ads/assign_edit', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}
		
		public function assign_save(){
			// load the form library validation and form helper
			$this->load->helper("form");
			$this->load->library('form_validation');
			
			// validate the start_date with date_check() defined rule
			$this->form_validation->set_rules('start_date', 'Start date', 'callback_date_check');

			$ads_id 			= $this->input->post('ads_id');
				
			// the validation return FALSE: get all related data again and display the input form again
			if ($this->form_validation->run() === FALSE){				
				$assign_id 		= $this->input->post('id');
				$data			= $this->assign_edit_prepare($ads_id, $assign_id);
				$data['title']	= "Assign";
				$this->load->view('templates/header1', $data);
				$this->load->view('ads/assign_edit', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			}
			else{
				$this->ads_assign_model->save();
				redirect("ads/assign_list".$ads_id, "refresh");
			}
		}
		
		public function assign_edit_prepare($ads_id, $id){
			$data = array();
			$data['ads_id'] 	= $ads_id;
			$data['ads_assign'] = $this->ads_assign_model->getAdsAssign($id);
			$data['start_date'] = date('Y-m-d', strtotime($data['ads_assign']['start_date']));
			$data['start_h'] 	= date('h', strtotime($data['ads_assign']['start_date']));
			$data['start_i'] 	= date('i', strtotime($data['ads_assign']['start_date']));
			$data['start_s'] 	= date('s', strtotime($data['ads_assign']['start_date']));
			
			$data['end_date'] 	= date('Y-m-d', strtotime($data['ads_assign']['end_date']));
			$data['end_h'] 		= date('h', strtotime($data['ads_assign']['end_date']));
			$data['end_i'] 		= date('i', strtotime($data['ads_assign']['end_date']));
			$data['end_s'] 		= date('s', strtotime($data['ads_assign']['end_date']));
			$data['title'] 		= "Assign";
			// load hour, minute, second
			$data['h'] 			= $this->getHours();
			$data['i']			= $this->getMins();
			$data['s']			= $this->getSecs();	
			return $data;
		}
		
		public function assign_delete($ads_id, $id){
			$this->ads_assign_model->delete($id);
			redirect("ads/assign_list/".$ads_id, "refresh");
		}
		
		public function broadcast(){
			$data['broadcast'] 	= $this->ads_broadcast_model->getBroadcastList();
			$data['title'] 		= "Broadcast list";
			$this->load->view('templates/header1', $data);
			$this->load->view('ads/broadcast_list', $data);
			$this->load->view('templates/footer');
		}
		
		public function broadcast_form($id=FALSE){
			// load the form library validation and form helper
			$this->load->helper("form");
			$this->load->library('form_validation');		
			$data = array();
			
			$data = $this->broadcast_prepare($id);
			
			$data['title'] 		= "Broadcast Create";
			$this->load->view('templates/header1', $data);
			$this->load->view('ads/broadcast_create', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}
		
		public function broadcast_create(){
			$this->broadcast_form();
		}
		
		public function broadcast_edit($id){
			$this->broadcast_form($id);
		}
		
		public function broadcast_prepare($id=FALSE){
			$data = array();
			$data['ads']			= $this->ads_model->getAdsList();
			
			if($id!=FALSE){
				$data['broadcast']  	= $this->ads_broadcast_model->getBroadcastList($id);		
				if(count($data['broadcast'])){
					$data['start_date'] = date('Y-m-d', strtotime($data['broadcast']['start_date']));
					$data['start_h'] 	= date('h', strtotime($data['broadcast']['start_date']));
					$data['start_i'] 	= date('i', strtotime($data['broadcast']['start_date']));
					$data['start_s'] 	= date('s', strtotime($data['broadcast']['start_date']));
					
					$data['end_date'] 	= date('Y-m-d', strtotime($data['broadcast']['end_date']));
					$data['end_h'] 		= date('h', strtotime($data['broadcast']['end_date']));
					$data['end_i'] 		= date('i', strtotime($data['broadcast']['end_date']));
					$data['end_s'] 		= date('s', strtotime($data['broadcast']['end_date']));
				}
			}
			
			// load hour, minute, second
			$data['h'] 			= $this->getHours();
			$data['i']			= $this->getMins();
			$data['s']			= $this->getSecs();	
			return $data;
		}
		
		public function broadcast_save(){
			// load the form library validation and form helper
			$this->load->helper("form");
			$this->load->library('form_validation');
			
			// validate the start_date with date_check() defined rule
			$this->form_validation->set_rules('start_date', 'Start date', 'callback_date_check');
			
			//validate the ads_id selection with required rule
			$this->form_validation->set_rules('ads_id', 'Ads ID', 'required');	

			//validate the status selection with required rule
			$this->form_validation->set_rules('status', 'Status', 'required');
			
			// the validation return FALSE: get all related data again and display the input form again
			if ($this->form_validation->run() === FALSE){				
				$id = $this->input->post('id');
				$data = $this->broadcast_prepare($id);
				$data['title']	= "Assign";
				$this->load->view('templates/header1', $data);
				$this->load->view('ads/broadcast_create', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			}
			else{
				$this->ads_broadcast_model->save();
				redirect("ads/broadcast".$ads_id, "refresh");
			}
			
		}
		
		public function broadcast_delete($id){
			$this->ads_broadcast_model->delete($id);
			redirect("ads/broadcast/", "refresh");
		}
	}
?>